/**
 * User: zsquirrel
 * Date: 2020/5/1
 * Time: 10:23 上午
 */
package com.cskaoyan.mall.dao;

import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.mall.model.Order;
import com.cskaoyan.mall.model.bo.ChangeOrderBO;
import com.cskaoyan.mall.model.bo.OrderRequestParamBO;
import com.cskaoyan.mall.model.enmu.OrderComment;
import com.cskaoyan.mall.model.vo.StateOrderVO;
import com.cskaoyan.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderDaoImpl implements OrderDao {
    @Override
    public Integer getTotal(OrderRequestParamBO param) {
        Map<String, Object> map = getDynamicSqlParams(param);
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        String baseSql = (String) map.get("baseSql");
        List sqlParams = (List) map.get("sqlParams");
        Long number = null;
        try {
            number = runner.query("select count(id) from orders " + baseSql, new ScalarHandler<>(), sqlParams.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return number.intValue();
    }

    private Map<String, Object> getDynamicSqlParams(OrderRequestParamBO param) {
        String baseSql = " where 1 = 1 ";
        List<Object> sqlParams = new ArrayList<>();
        if(param.getState() != -1){
            baseSql = baseSql + " and stateId = ? ";
            sqlParams.add(param.getState());
        }
        if(!StringUtils.isEmpty(param.getMoneyLimit1())){
          baseSql = baseSql + " and amount <= ?";
          sqlParams.add(Double.parseDouble(param.getMoneyLimit1()));
        }
        if(!StringUtils.isEmpty(param.getMoneyLimit2())){
            baseSql = baseSql + " and amount >= ?";
            sqlParams.add(Double.parseDouble(param.getMoneyLimit2()));
        }
        if(!StringUtils.isEmpty(param.getGoods())){
           baseSql = baseSql + " and goods like ?";
           sqlParams.add("%" + param.getGoods() + "%");
        }
        if(!StringUtils.isEmpty(param.getAddress())){
            baseSql = baseSql + " and address like ?";
            sqlParams.add("%" + param.getAddress() + "%");
        }
        if(!StringUtils.isEmpty(param.getName())){
            baseSql = baseSql + " and name like ?";
            sqlParams.add("%" + param.getName() + "%");
        }
        if(!StringUtils.isEmpty(param.getId())){
            baseSql = baseSql + " and id = ?";
            sqlParams.add(param.getId());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("baseSql", baseSql);
        map.put("sqlParams", sqlParams);
        return map;
    }

    @Override
    public List<Order> ordersByPage(OrderRequestParamBO param) {
        Map<String, Object> map = getDynamicSqlParams(param);
        String baseSql = (String) map.get("baseSql");
        List sqlParams = (List) map.get("sqlParams");
        //还需要处理limit ？ offset ?
        String sql = "select * from orders " + baseSql + "limit ? offset ?";
        sqlParams.add(param.getPagesize());
        sqlParams.add((param.getCurrentPage() - 1) * param.getPagesize());
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Order> orders = null;
        try {
            orders = runner.query(sql, new BeanListHandler<Order>(Order.class), sqlParams.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return orders;
    }

    @Override
    public Order getOrderInfo(String id) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Order order = null;
        try {
            order = runner.query("select * from orders where id = ?", new BeanHandler<>(Order.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return order;
    }

    @Override
    public void changeOrder(ChangeOrderBO changeOrderBO) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("update orders set stateId = ? where id = ?",
                    changeOrderBO.getState(),
                    changeOrderBO.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteOrder(String id) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("delete from orders where id = ?", id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void placeOrder(Order order) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("insert into orders values (null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                    order.getUserId(),
                    order.getGoodsDetailId(),
                    order.getGoods(),
                    order.getSpec(),
                    order.getGoodsNum(),
                    order.getAmount(),
                    order.getStateId(),
                    order.getNickname(),
                    order.getName(),
                    order.getAddress(),
                    order.getPhone(),
                    order.getUnitPrice(),
                    order.getImg(),
                    OrderComment.NO.getCode(),
                    order.getGoodsId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<StateOrderVO> getOrderByState(String state, Integer userId) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        //runner.query("")
        return null;
    }
}
